using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;

namespace SqlRunner.UserInterfaceStatus
{
	/// <summary>
	/// Groups UI controls so a common property can be easily modified
	/// using a binary flag value
	/// </summary>
	public class GroupControls
	{

		#region Private Instances

		private readonly string _property;
		private readonly Status _flag;
		private ArrayList _controls;

		#endregion
		#region Public Methods

		public GroupControls(string property, Status flag)
		{
			this._property = property;
			this._flag = flag;
			_controls = new ArrayList();
		}

		public void AddControl(Component control)
		{
			Type t = control.GetType();
			IEnumerator iterator = t.GetProperties().GetEnumerator();
			bool isFound = false;
			
			while ( !isFound & iterator.MoveNext())
			{
				PropertyInfo property = (PropertyInfo) iterator.Current;
				if ( property.Name.Equals(_property) )
				{
					_controls.Add(control);
					isFound = true;
				}
			}
		}

		public void UpdateProperty(Status status)
		{
			bool value = (_flag & status) == status? true : false; 
			foreach (Component control in _controls)
			{
				Type t = control.GetType();
				PropertyInfo property = t.GetProperty(_property);
				property.SetValue(control, value, null);
			}
		}
		#endregion

	}
}
